package com.egao.common.module.student.service.impl;

import com.egao.common.module.student.entity.StudentResumeItem;
import com.egao.common.module.student.mapper.StudentResumeItemMapper;
import com.egao.common.module.student.service.StudentResumeItemService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.exception.ParameterException;

import java.util.List;

/**
 * (StudentResumeItem)表服务实现类
 *
 * @author dl
 * @since 2021-05-27 18:27:16
 */
@Service
public class StudentResumeItemServiceImpl extends ServiceImpl<StudentResumeItemMapper, StudentResumeItem> implements StudentResumeItemService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public JsonResult queryById(Integer id) {
        StudentResumeItem studentResumeItem = verifyStudentResumeItem(id);
        return JsonResult.ok().setData(studentResumeItem);
    }

    /**
     * 查询多条数据
     *
     * @param page 查询条件
     * @return 对象列表
     */
    @Override
    public List<StudentResumeItem> listPage(PageParam<StudentResumeItem> page) {
        return baseMapper.listPage(page);
    }

    /**
     * 新增数据
     *
     * @param studentResumeItem 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult insert(StudentResumeItem studentResumeItem) {
        if (save(studentResumeItem)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    /**
     * 修改数据
     *
     * @param studentResumeItem 实例对象
     * @return 实例对象
     */
    @Override
    public JsonResult update(StudentResumeItem studentResumeItem) {
        StudentResumeItem old = verifyStudentResumeItem(studentResumeItem.getId());
        if (old == null) {
            throw new BusinessException("不存在");
        }
        if (updateById(studentResumeItem)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public JsonResult deleteById(Integer id) {
        StudentResumeItem old = verifyStudentResumeItem(id);
        if (old == null) {
            throw new BusinessException("不存在");
        }
        if (removeById(old)) {
            return JsonResult.ok("提交成功");
        }
        return JsonResult.error("提交失败");
    }

    /**
     * 检查是否存在
     *
     * @param id 编号
     * @return StudentResumeItem
     */
    private StudentResumeItem verifyStudentResumeItem(Integer id) {
        if (id == null) {
            throw new ParameterException("编号为空");
        }

        StudentResumeItem studentResumeItem = baseMapper.selectById(id);
        if (studentResumeItem == null) {
            throw new BusinessException("不存在");
        }
        return studentResumeItem;
    }

    /**
     * 检查参数
     *
     * @param studentResumeItem
     */
    private void verifyStudentResumeItemParam(StudentResumeItem studentResumeItem) {
        if (null == null) {
            throw new ParameterException("编号为空");
        }
        if (null == null) {
            throw new BusinessException("编号为空");
        }
    }


}
